import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class APP {

    static Scanner scanner = new Scanner(System.in);
    static List<Student> students = new ArrayList();
    static File file = new File("C:\\Users\\mamengwei\\Desktop\\java_xiangmu\\学生管理系统功能升级\\src\\student.txt");

    public static void main(String[] args) {
        try {
            initStudents();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        while (true) {
            System.out.println("-----欢迎来到学生管理系统-----");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入:");
            int select = scanner.nextInt();
            switch (select) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    deleteStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    showStudent();
                    break;
                case 5:
                    System.exit(0);
                    break;
            }
        }
    }




    static void addStudent() {

        System.out.println("请输入学生id:");
        int id = scanner.nextInt();
        System.out.println("请输入学生姓名:");
        String name = scanner.next();
        System.out.println("请输入学生年龄:");
        int age = scanner.nextInt();
        System.out.println("请输入学生家庭住址:");
        String address = scanner.next();
        System.out.println("请输入学生QQ:");
        String QQ = scanner.next();
        System.out.println("请输入学生电话:");
        String phone = scanner.next();
        Student student = new Student(id, name, age, address,QQ,phone);
        students.add(student);
        try {
            flush(students);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("添加成功!");
    }

    static void deleteStudent() {
        if (students.size() == 0) {
            System.out.println("当前无学生信息,请添加后在查询");
            return;
        }
        showStudent();
        System.out.println("请输入要删除学生的ID:");
        int id = scanner.nextInt();
        for (Student stu :
                students) {
            if (stu.id == id) {
                students.remove(stu);
                try {
                    flush(students);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("id不存在");
    }

    static void updateStudent() {
        if (students.size() == 0) {
            System.out.println("当前无学生信息,请添加后在查询");
            return;
        }
        showStudent();
        System.out.println("请输入要修改学生的id:");
        int id = scanner.nextInt();
        System.out.println("请输入把该学生的姓名修改为:");
        String name = scanner.next();
        System.out.println("请输入把该学生的年龄修改为:");
        int age = scanner.nextInt();
        System.out.println("请输入把该学生的家庭住址修改为:");
        String address = scanner.next();
        System.out.println("请输入把该学生的QQ修改为:");
        String QQ = scanner.next();
        System.out.println("请输入把该学生的phone修改为:");
        String phone = scanner.next();
        for (Student stu :
                students) {
            if (stu.id == id) {
                stu.name = name;
                stu.age = age;
                stu.address = address;
                stu.QQ = QQ;
                stu.phone = phone;
                System.out.println("修改成功!");
                try {
                    flush(students);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                return;
            }
        }
        System.out.println("此id不存在!");
    }

    static void showStudent() {
        if (students.size() == 0) {
            System.out.println("当前无学生信息,请添加后在查询");
            return;
        }
        for (Student stu :
                students) {
            System.out.println(stu);
        }
    }

    static void flush(List<Student> students) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        for (Student student :
                students) {
            StringBuilder sb = new StringBuilder();
            sb.append(student.id).append(" ").append(student.name).append(" ").append(student.age)
                    .append(" ").append(student.address).append(" ").append(student.QQ).append(" ").append(student.phone);
            bw.write(sb.toString());
            bw.newLine();
            bw.flush();
        }
    }

    private static void initStudents() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String len;
        while ((len = br.readLine()) != null){
            String[] data = len.split(" ");

            Student student = new Student(Integer.parseInt(data[0]), data[1], Integer.parseInt(data[2]), data[3],data[4],data[5]);
            students.add(student);
        }
    }
}
